package com.lab.network.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.cuitrip.apiservice.BusinessHelper;
import com.cuitrip.app.MainApplication;
import com.cuitrip.app.base.UnitUtils;
import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.util.I118Util;
import com.lab.rtenvironment.AppCoreRuntime;
import com.umeng.message.UmengRegistrar;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import static android.os.Build.BRAND;
import static android.os.Build.MODEL;
import static android.os.Build.VERSION.SDK_INT;

public class CtApiRequest {

    private LinkedHashMap<String, Object> dataParams;

    private String method;
    private int type = 0;
    /**
     * 调用的api名称
     */
    private String apiName;

    /**
     * 调用的api的url
     */
    private String apiURL;

    public CtApiRequest() {
        apiURL = BusinessHelper.API_URL;
        CtUserInfo info = LoginInstance.getInstance().getUserInfo();
        if (info != null) {
            addParam("uid", info.getUid());
            addParam("token", info.getToken());
        }
        //addParam("language", I118Util.getLanguageTag());
        addParam("lang", I118Util.getLanguageTag());
        addCurrencyParam();
        addParam("clientVersion", MainApplication.getInstance().getVersionName());
        addParam("tid", UmengRegistrar.getRegistrationId(AppCoreRuntime.context));
        addParam("version", BusinessHelper.API_VERSION);
        addParam("osVersion", SDK_INT);
        addParam("machine", new StringBuilder(BRAND).append(MODEL).toString());
        addParam("client", "android");
    }

    public void addCurrencyParam() {
        String currency = UnitUtils.getSettingMoneyType();
        if (!TextUtils.isEmpty(currency)) {
            addParam("showCurrency", currency.toUpperCase());
        }
    }

    public void removeCurrencyParam() {
        if (dataParams != null && dataParams.containsKey("showCurrency")) {
            dataParams.remove("showCurrency");
        }
    }

    public void removeUserInfoParam() {
        if (dataParams != null && dataParams.containsKey("uid") && dataParams.containsKey("token")) {
            dataParams.remove("uid");
            dataParams.remove("token");
        }
    }

    /**
     * 新增请求参数
     *
     * @param key   请求键值
     * @param param 请求参数属性值
     */
    public void addParam(String key, Object param) {
        if (dataParams == null) {
            dataParams = new LinkedHashMap<String, Object>();
        }
        dataParams.put(key, param);
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public LinkedHashMap<String, Object> getDataParams() {
        return dataParams;
    }

    /**
     * @return the apiName
     */
    public String getApiName() {
        return apiName;
    }

    /**
     * @param apiName the apiName to set
     */
    public void setApiName(String apiName) {
        this.apiName = apiName;
    }

    public String toAsJsonString() {
        HashMap<String, Object> dataParams = getDataParams();
        Iterator iterator = dataParams.entrySet().iterator();

        JSONObject jsonObject = new JSONObject();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();
            Object value = entry.getValue();
            /*if (value instanceof String) {
                value = EmojiFilterUtils.filterEmoji((String) value);
            }*/
            jsonObject.put(entry.getKey(), value);
        }
        return jsonObject.toJSONString();
    }

}
